package com.nowcoder.community.controller;
import com.nowcoder.community.annotation.LoginRequire;
import com.nowcoder.community.controller.exception.PageErrorException;
import com.nowcoder.community.controller.exception.PageNotFoundException;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.util.Constants;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    HostHolder hostHolder;

    @LoginRequire
    @PostMapping("/add/{postId}")
    public String comment(@PathVariable int postId, Comment comment) {
        comment.setUserId(hostHolder.getUser().getId());
        comment.setCreateTime(new Date());
        commentService.addPostComment(postId, comment);
        return "redirect:/discuss/detail/" + postId;
    }
}
